﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using VH.SharePoint.Api.MetaWeblog;
using VH.SharePoint.Api.Queries;

namespace VH.SharePoint.Api.SharePoint
{
    public class SPMediaRepository : MediaRepository
    {
        public BlogWeb Blog { get; private set; }
        public SPMediaRepository(BlogWeb blog)
        {
            Blog = blog;
        }
        public override IEnumerable<MediaObjectInfo> FindAll()
        {
            var medias = Blog.GetMedias();
            return medias.Select(m => GetMediaObjectFromBlog(m)).ToList();
        }

        public override IEnumerable<MediaObjectInfo> FindAll(QueryConstraints<MediaObjectInfo> constraints)
        {
            var medias = Blog.GetMedias();
            medias.Query.RowLimit = (uint)constraints.PageSize;
            return constraints.ApplyTo(medias.Select(m => GetMediaObjectFromBlog(m)).AsQueryable()).ToList();
        }

        public override MediaObjectInfo Get(string id)
        {
            var medias = Blog.GetMedias();
            var media = medias.FirstOrDefault( m => m.Name.Equals(id, StringComparison.InvariantCultureIgnoreCase));
            return GetMediaObjectFromBlog(media);
        }

        public override void Create(MediaObjectInfo entity, byte[] mediaContent)
        {
            var medias = Blog.GetMedias();
            var mediaInfo = new MediaItemInforCreation();
            mediaInfo.Content = mediaContent;
            mediaInfo.MimeType = entity.MimeType;
            mediaInfo.Overwrite = true;
            mediaInfo.Name = entity.FileName;
            var media = medias.Add(mediaInfo);
            entity.Url = media.Url;
            entity.Id = media.Id.ToString(CultureInfo.InvariantCulture);
        }

        public override void Update(MediaObjectInfo entity)
        {
            var medias = Blog.GetMedias();
            var media = medias[int.Parse(entity.Id)];
            media.Name = entity.FileName;
            media.Update();
        }

        public override void Delete(MediaObjectInfo entity)
        {
            var medias = Blog.GetMedias();
            if (!string.IsNullOrEmpty(entity.Id))
            {
                medias.Delete(int.Parse(entity.Id));
                return;
            }
            var media = medias.GetByFileName(entity.FileName);
            medias.Delete(media.Id);
        }

        public override void ValidateUser(string username, string password)
        {
            
        }

        public override void Update(MediaObjectInfo entity, byte[] mediaContent)
        {
            var medias = Blog.GetMedias();
            var media = medias[int.Parse(entity.Id)];
            media.Name = entity.FileName;
            media.MimeType = entity.MimeType;
            media.Update(mediaContent);
        }

        private MediaObjectInfo GetMediaObjectFromBlog(MediaItem item)
        {
            var res = new MediaObjectInfo();
            res.Id = item.Id.ToString(CultureInfo.InvariantCulture);
            res.Url = item.Url;
            res.FileName = item.Name;
            res.MimeType = item.MimeType;
            return res;
        }
    }
}
